<html>
<head>
<title>Using the Twisted Application Framework</title>
</head>
<body>

<h1>Using the Twisted Application Framework</h1>

<h2>Introduction</h2>

<h3>Audience</h3>

The target audience of this document is a Twisted user who wants to deploy a
significant amount of Twisted code in a re-usable, standard and easily
configurable fashion.  A Twisted user who wishes to use the Application
framework needs to be familiar with developing Twisted <a
href="servers.xhtml">servers</a> and/or <a href="clients.xhtml">clients</a>.

<h3>Goals</h3>

<ul>
  <li>To introduce the Twisted Application infrastructure.</li>
  
  <li>To explain how to deploy your Twisted application using <code>.tac</code>
  files and <code>twistd</code></li>

  <li>To outline the existing Twisted services.</li>
</ul>

<h2>Overview</h2>

<p>The Twisted Application infrastructure takes care of running and stopping
your application.  Using this infrastructure frees you from from having to
write a large amount of boilerplate code by hooking your application into
existing tools that manage daemonization, logging, <a
href="choosing-reactor.xhtml">choosing a reactor</a> and more.</p>

<p>The major tool that manages Twisted applications is a command-line utility
called <code>twistd</code>.  <code>twistd</code> is cross platform, and is the
recommended tool for running Twisted applications.  </p>


<p>The core component of the Twisted Application infrastructure is the <code
class="API">twisted.application.service.Application</code> object &mdash; an
object which represents your application.  However, Application doesn't provide
anything that you'd want to manipulate directly.  Instead, Application acts as
a container of any <q>Services</q> (objects implementing <code class="API"
base="twisted.application.service">IService</code>) that your application
provides.  Most of your interaction with the Application infrastructure will be
done through Services.</p>

<p>By <q>Service</q>, we mean anything in your application that can be started
and stopped.  Typical services include web servers, FTP servers and SSH
clients.  Your Application object can contain many services, and can even
contain structured heirarchies of Services using <code class="API"
base="twisted.application.service">IServiceCollection</code>s.</p>

<p>Here's a simple example of constructing an Application object which
represents an echo server that runs on TCP port 7001.</p>

<pre class="python">
from twisted.application import internet, service
from somemodule import EchoFactory

port = 7001
factory = EchoFactory()

# this is the important bit
application = service.Application("echo")  # create the Application
echoService = internet.TCPServer(port, factory) # create the service
# add the service to the application
echoService.setServiceParent(application)
</pre>

<p>See <a href="servers.xhtml">Writing Servers</a> for an explanation of
EchoFactory.</p>

<p>This example creates a simple heirarchy:
<pre>
   application
   |
   `- echoService
</pre> More complicated heirarchies of services can be created using
IServiceCollection.  You will most likely want to do this to manage Services
which are dependent on other Services.  For example, a proxying Twisted
application might want its server Service to only start up after the associated
Client service. </p>


<h2>Using application</h2>

<h3>twistd and tac</h3><a name="twistd" />

<p>To handle start-up and configuration of your Twisted application, the
Twisted Application infrastructure uses <code>.tac</code> files.
<code>.tac</code> are Python files which configure an <code class="API"
base="twisted.application.service">Application</code> object and assign this
object to the top-level variable <q><code>application</code></q>.</p>

<p>The following is a simple example of a <code>.tac</code> file:</p>

<a href="listings/application/service.tac" class="py-listing">service.tac</a>

<p><code>twistd</code> is a program that runs Twisted applications using a
<code>.tac</code> file. In its most simple form, it takes a single argument
<code>-y</code> and a tac file name. For example, you can run the above server
with the command <code class="shell">twistd -y service.tac</code>.</p>

<p>By default, <code>twistd</code> daemonizes and logs to a file called
<code>twistd.log</code>. More usually, when debugging, you will want your
application to run in the foreground and log to the command line. To run the
above file like this, use the command <code class="shell">twistd -noy
service.tac</code></p>

<p>For more information, see the <code>twistd</code> man page.</p>

<h3>Services provided by Twisted</h3>

<p>Twisted provides several services that you want to know about.</p>

<p>Each of these services (except TimerService) has a corresponding
<q>connect</q> or <q>listen</q> method on the reactor, and the constructors for
the services take the same arguments as the reactor methods.  The
<q>connect</q> methods are for clients and the <q>listen</q> methods are for
servers.  For example, TCPServer corresponds to reactor.listenTCP and TCPClient
corresponds to reactor.connectTCP.  </p>

<dl>
  <dt><code class="API"
    base="twisted.application.internet">TCPServer</code>
  </dt>
    
  <dt><code class="API"
    base="twisted.application.internet">TCPClient</code>
  </dt>
      
  <dd>
    Services which allow you to make connections and listen for connections
    on TCP ports.
    <ul>
      <li><code class="API"
      base="twisted.internet.interfaces.IReactorTCP">listenTCP</code></li>
      <li><code class="API"
      base="twisted.internet.interfaces.IReactorTCP">connectTCP</code></li>
    </ul>
  </dd>
  
  <dt><code class="API"
  base="twisted.application.internet">UNIXServer</code></dt>

  <dt><code class="API"
  base="twisted.application.internet">UNIXClient</code></dt>

  <dd>
    Services which listen and make connections over UNIX sockets.
    <ul>
      <li><code class="API"
      base="twisted.internet.interfaces.IReactorUNIX">listenUNIX</code></li>
      <li><code class="API"
      base="twisted.internet.interfaces.IReactorUNIX">connectUNIX</code></li>
    </ul>
  </dd>  

  <dt><code class="API"
  base="twisted.application.internet">SSLServer</code></dt>

  <dt><code class="API"
  base="twisted.application.internet">SSLClient</code></dt>

  <dd>Services which allow you to make SSL connections and run SSL servers.
    <ul>
      <li><code class="API"
      base="twisted.internet.interfaces.IReactorSSL">listenSSL</code></li>
      <li><code class="API"
      base="twisted.internet.interfaces.IReactorSSL">connectSSL</code></li>
    </ul>
  </dd>  

  <dt><code class="API"
  base="twisted.application.internet">UDPServer</code></dt>

  <dt><code class="API"
  base="twisted.application.internet">UDPClient</code></dt>

  <dd>Services which allow you to send and receive data over UDP
    <ul>
      <li><code class="API"
      base="twisted.internet.interfaces.IReactorUDP">listenUDP</code></li>
      <li><code class="API"
      base="twisted.internet.interfaces.IReactorUDP">connectUDP</code></li>
    </ul>

    <p>See also the <a href="udp.xhtml">UDP documentation</a>.</p>
  </dd>

  <dt><code class="API"
  base="twisted.application.internet">UNIXDatagramServer</code></dt>

  <dt><code class="API"
  base="twisted.application.internet">UNIXDatagramClient</code></dt>

  <dd>Services which send and receive data over UNIX datagram sockets.
    <ul>
      <li><code class="API"
      base="twisted.internet.interfaces.IReactorUDP">listenUNIXDatagram</code></li>
      <li><code class="API"
      base="twisted.internet.interfaces.IReactorUDP">connectUNIXDatagram</code></li>
    </ul>
  </dd>

  <dt><code class="API"
  base="twisted.application.internet">MulticastServer</code></dt>

  <dd>
    A server for UDP socket methods that support multicast.
    <ul>
      <li><code class="API"
      base="twisted.internet.interfaces.IReactorUDP">listenMulticast</code></li>
    </ul>
  </dd>
  
  <dt><code class="API"
  base="twisted.application.internet">TimerService</code></dt>

  <dd>
    A service to periodically call a function.
  </dd>
  
</dl>

<h3>Service Collection</h3>

<p><code class="API"
base="twisted.application.service">IServiceCollection</code> objects contain
<code class="API" base="twisted.application.service">IService</code> objects.
IService objects can be added to IServiceCollection by calling <code class="API"
base="twisted.application.service.IService">setServiceParent</code> and detached
by using <code class="API"
base="twisted.application.service.IService">disownServiceParent</code>.</p>

<p>The standard implementation of IServiceCollection is <code class="API"
base="twisted.application.service">MultiService</code>, which also implements
IService.  MultiService is useful for creating a new Service which combines two
or more existing Services.  For example, you could create a DNS Service as a
MultiService which has a TCP and a UDP Service as children.</p>

<pre class="python">
from twisted.application import internet, service
from twisted.names import server, dns, hosts

port = 53

# Create a MultiService, and hook up a TCPServer and a UDPServer to it as
# children.
dnsService = service.MultiService()
hostsResolver = hosts.Resolver('/etc/hosts')
tcpFactory = server.DNSServerFactory([hostsResolver])
internet.TCPServer(port, tcpFactory).setServiceParent(dnsService)
udpFactory = dns.DNSDatagramProtocol(tcpFactory)
internet.UDPServer(port, udpFactory).setServiceParent(dnsService)

# Create an application as normal
application = service.Application("DNSExample")

# Connect our MultiService to the application, just like a normal service.
dnsService.setServiceParent(application)
</pre>

</body>

</html>
